var currEmployee = {
    id: '',
    loginName: '',
    mobile: '',
    idNumber: ''
};
var employeeId = $("input[name=id]").val();
$.get("department/getAll", function (data) {
    if (data.totalSize > 0) {
        for (var i = 0; i < data.totalSize; i++) {
            var d = data.result[i];
            $("#department").append($("<option></option>").attr("value", d.id).text(d.name));
        }
    }
    if (employeeId && employeeId !== '') {
        $.get("employee/getById/" + employeeId, function (employee) {
            if (employee) {
                $("#loginName").val(employee.loginName);
                $("#mobile").val(employee.mobile);
                $("#name").val(employee.name);
                $("#idNumber").val(employee.idNumber);
                if (employee.departmentId && employee.departmentId !== '') {
                    $("#department").val(employee.departmentId);
                }
                currEmployee = employee;
            }
        });
    }
});
/**
 * 初始化表单验证
 * 
 * @param {type} param
 */
$("#employeeForm").validate({
    //错误类名和元素标签类型
    errorClass: "gg-error",
    validClass: "gg-success",
    errorElement: "label",
    //验证规则正则表达式
    rules: {
        loginName: {
            required: true,
            regx: /^[a-zA-Z0-9_\u4e00-\u9fb9]{2,30}$/,
            remote: "employee/isLoginNameAvailable?&id=" + employeeId
        },
        mobile: {
            required: true,
            regx: /^1\d{10}$/,
            remote: "employee/isMobileAvailable?id=" + employeeId
        },
        name: {
            required: true,
            regx: /^[\u4e00-\u9fa5]{2,10}$/
        },
        idNumber: {
            required: true,
            regx: /^[1-9][0-9]{16}[0-9xX]$/,
            remote: "employee/isIdNumberAvailable?id=" + employeeId
        }
    },
    //验证结果文字提示
    messages: {
        loginName: {
            required: '必填',
            regx: '格式不正确',
            remote: '已存在'
        },
        mobile: {
            required: '必填',
            regx: '格式不正确',
            remote: '已存在'
        },
        name: {
            required: '必填',
            regx: '格式不正确'
        },
        idNumber: {
            required: '必填',
            regx: '格式不正确',
            remote: '已存在'
        }
    },
    //验证结果处理
    highlight: function (element, errorClass, validClass) {
        $(element).closest('.form-group').addClass(errorClass).removeClass(validClass);
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).closest('.form-group').removeClass(errorClass).addClass(validClass);
    },
    //错误信息位置
    errorPlacement: function (error, element) {
        error.insertAfter(element);
    }
});

/**
 * 提交表单
 * 
 * @param {type} param
 */
$("#saveEmployeeBtn").click(function () {
    if ($("#employeeForm").valid()) {
        var data = $("#employeeForm").formToJson();
        $.post("employee/save", data, function (res) {
            if (res.success) {
                alert(res.message);
                location.href = "employee/list";
            } else {
                alert(res.message);
            }
        }).fail(function () {
            alert("网络通信错误");
        });
    }
});